<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

class TestController extends CommonController{

    public function find(Request $request)
    {
        $url = $request->input('url', '');
        if (!$url) {
            return $this->output(10005, '非法URL');
        }
        $content = file_get_contents($url);
        if (!$content) {
            return $this->output(10005, '无法下载内容');
        }
        list($trash, $content) = explode('<div id="venueMap"', $content);
        unset($trash);
        preg_match("/data\-src=\"(.*?map.svg?.*?)\"/im", $content, $matches);
        if ($matches && $matches[1]) {
            $src = $matches[1];
        } else {
            return $this->output(10005, '无法找到匹配的电子地图');
        }
        $content = file_get_contents($src);

        $dir = ROOT_PATH.'public'.DS.'uploads'.DS;
        $subdir = date('Ymd').DS;
        $savename = $subdir.md5(microtime(true)).'.svg';
        if (!is_dir($dir.$subdir)) {
            if (!mkdir($dir.$subdir, 0755, true)) {
                return $this->output(10005, '创建目录失败');
            }
        }
        $re = file_put_contents($dir.$savename, $content);
        if (false === $re) {
            return $this->output(10005, '保存文件出错');
        }
        return $this->output(0, '', [
            'savename' => $savename,
        ]);
    }

    public function finde(Request $request)
    {
        $url = $request->input('url', '');
        if (!$url) {
            return $this->output(10005, '非法URL');
        }
        // $context = stream_context_create(array(
        //     'http' => array(
        //         'timeout' => 5, //超时时间，单位为秒
        //     ),
        // ));
        // $content = file_get_contents($url, false, $context);
        // $content = file_get_contents($url);

        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);  // 设置要抓取的页面地址
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 抓取结果直接返回（如果为0，则直接输出内容到页面）
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)'); // 模拟用户使用的浏览器
        @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($ch, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
        curl_setopt($ch, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
        curl_setopt($ch, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        $content = curl_exec($ch);

        // 4. 释放curl句柄
        curl_close($ch);
        if (!$content) {
            return $this->output(10005, '无法下载内容', ['error' => curl_error($ch)]);
        }

        list($trash, $content) = explode('<div id="venueMap"', $content);
        unset($trash);
        preg_match("/data\-src=\"(.*?map.svg?.*?)\"/im", $content, $matches);
        if ($matches && $matches[1]) {
            $src = $matches[1];
        } else {
            return $this->output(10005, '无法找到匹配的电子地图');
        }
        // $src = 'https://intl.stubhub.com/events/356793/map.svg?width=&height=&newmap=1&ts=1517915326000';
        $content = file_get_contents($src);

        $dir = ROOT_PATH.'public'.DS.'uploads'.DS;
        $subdir = date('Ymd').DS;
        $savename = $subdir.md5(microtime(true)).'.svg';
        if (!is_dir($dir.$subdir)) {
            if (!mkdir($dir.$subdir, 0755, true)) {
                return $this->output(10005, '创建目录失败');
            }
        }
        $re = file_put_contents($dir.$savename, $content);
        if (false === $re) {
            return $this->output(10005, '保存文件出错');
        }

        return $this->output(0, '', [
            'savename' => $savename,
        ]);
    }
}